<?php
/**
 * User: @lixu
 * Date: 2019-02-18
 * Time: 16:30
 */

namespace Biz\module\Classes\adm;

use Biz\model\activity\PinTuan as mPinTuan;
use Biz\model\activity\PinTuanSku as mPinTuanSku;
use Biz\service\activity\AllactivityInfoBak;
use Biz\service\Goods as sGoods;
use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;
use Biz\pockets\constInterface\ActivityInfo;
use Doraemon\pockets\constInterface\AppType;
use Biz\service\activity\AllActivityInfo;
use Biz\service\Sku as sSku;
use Biz\model\activity\AllActivitySku as mAllActivitySku;
use Biz\tools\CheckActivityTime as tCheckActivityTime;
use Biz\pockets\excel\ReadExcel;
use Biz\pockets\excel\DownExcel;

class PinTuan
{
    const APP_TYPE = [
        AppType::APPTYPE_H5,
        AppType::APPTYPE_IOS,
        AppType::APPTYPE_ANDROID,
        AppTYpe::APPTYPE_MINIPROGRAM,
        AppTYpe::APPTYPE_COMMUNITY_MINIPROGRAM
    ];

    /**
     * 拼团列表
     * @param array $params
     * @return array
     */
    public static function getPinTuanList($params = [])
    {
        $skuList = [];
        $params = array_filter($params);
        $pinTuanList = mPinTuan::getList($params);
        $pinTuanIds = array_column($pinTuanList, 'id');
        $goodsIds = array_column($pinTuanList, 'goods_id');
        $skuInfo = sSku::Sku($goodsIds, sSku::FIELDS_GROUP_ALL);
        $goodsInfo = sGoods::goods($goodsIds, sGoods::FIELDS_GROUP_BASE);
        foreach ($skuInfo as $goodsId => $sku) {
            foreach ($sku['sku_info'] as $info) {
                $skuList[$goodsId][$info['sku_id']] = $info;
            }
        }

        $pinTuanSkuList = mPinTuanSku::getList(['pintuan_id' => $pinTuanIds, 'status' => mPinTuanSku::STATUS_NORMAL]);
        foreach ($pinTuanList as $key => &$pinTuan) {
            $pinTuan['sku_nums'] = 0;
            $pinTuan['stocks'] = 0;
            $pinTuan['activity_allowance'] = 0;
            $pinTuan['group_cycle'] = $pinTuan['group_cycle'] / 3600 . '小时';
            foreach ($pinTuanSkuList as $pinTuanSku) {
                if ($pinTuan['id'] == $pinTuanSku['pintuan_id']) {
                    $pinTuan['sku_nums'] = $pinTuan['sku_nums'] + 1;
                    $pinTuan['stocks'] = $pinTuan['stocks'] + $pinTuanSku['activity_stock'];
                    if ($pinTuan['type'] == mPinTuan::TYPE_NEW && isset($skuList[$pinTuan['goods_id']][$pinTuanSku['sku_id']])) {
                        $pinTuan['activity_allowance'] += ($skuList[$pinTuan['goods_id']][$pinTuanSku['sku_id']]['ori_price'] * $pinTuanSku['activity_stock']) - ($pinTuanSku['activity_price'] * $pinTuanSku['activity_stock']);
                    }

                }
            }
            $pinTuan['title'] = $goodsInfo[$pinTuan['goods_id']]['title'];
            $pinTuan['pintuan_num'] = $pinTuan['stocks'] / $pinTuan['group_member_num'];
        }

        return ['list' => $pinTuanList, 'total' => mPinTuan::getCount($params)];
    }

    /**
     * 拼团sku列表
     * @param $params
     * @return array
     */
    public static function getPinTuanSkuList($params)
    {
        $list = mPinTuanSku::getList($params);

        $goodsIds = array_column($list, 'goods_id');
        $skuInfo = sSku::Sku($goodsIds, sSku::FIELDS_GROUP_ALL);

        foreach ($list as $key => $info) {
            foreach ($skuInfo[$goodsIds[0]]['sku_info'] as $item) {
                if ($item['sku_id'] == $info['sku_id']) {
                    $list[$key]['sku_name'] = $item['sku_desc'];
                }
            }
        }

        return ['list' => $list, 'total' => 10];
    }

    /**
     * 修改拼图基础属性
     * @param $id
     * @param $goodsId
     * @param $data
     * @param $adminId
     * @return bool
     * @throws \Exception
     */
    public static function savePinTuan($id, $goodsId, $data, $adminId)
    {
        $goodsInfo = sGoods::goods([$goodsId], sGoods::FIELDS_GROUP_BASE);

        if (empty($goodsInfo)) {
            throw new \Exception('商品不存在');
        }

        if (!in_array($goodsInfo[$goodsId]['status'], mGoodsBasicInfo::STATUS_APP_NORMAL_MAP)) {
            throw new \Exception('请检查商品状态');
        }
        $where = ['goods_id' => $goodsId, 'status' => mPinTuan::STATUS_NORMAL, 'end_time[>=]' => date('Y-m-d H:i:s')];
        if($id > 0) {
            $where = array_merge($where, ['id[!]' => $id]);
        }

        $checkList = mPinTuan::getList($where);
        foreach ($checkList as $info) {
            $result = tCheckActivityTime::rules(['start_time' => $info['start_time'], 'end_time' => $info['end_time']], ['start_time' => $data['start_time'], 'end_time' => $data['end_time']]);
            if($result['invalidWhereOne'] || $result['invalidWhereTwo'] || $result['invalidWhereThree'] || $result['invalidWhereFour']) {
                throw new \Exception('商品已入选了活动' . $info['id'] . ' 与本活动的时间重叠，不能加入');
            }
        }

        if ($id > 0) {

            $allActivityGoodsInfo = self::formatGoodsInfo($id, $goodsId, $goodsInfo[$goodsId]['shop_id'], $data);
            $result = mPinTuan::action(function () use ($id, $adminId, $allActivityGoodsInfo, $data) {
                $data['edittime'] = date('Y-m-d H:i:s');
                $data['admin_id'] = $adminId;
                $id = mPinTuan::save($id, $data);
                if (!$id) {
                    return false;
                }

                if (mPinTuanSku::getInfo(['pintuan_id' => $id, 'status' => mPinTuan::STATUS_NORMAL, 'activity_price[>]' => 0])) {
                    if (!mPinTuanSku::saveByPinTuanId($id, ['start_time' => $data['start_time'], 'end_time' => $data['end_time']])) {
                        return false;
                    }

                    $result = AllActivityInfo::admUpdateActivityAttr($id, AllActivityInfo::ACTIVITY_TYPE_PINTUAN, $allActivityGoodsInfo, AllActivityInfo::ALL_ACTIVITY_TYPE_PINTUAN);
                    return $result;
                }
                return true;
            });
        } else {
            $data['shop_id'] = $goodsInfo[$goodsId]['shop_id'];
            $data['addtime'] = date('Y-m-d H:i:s');
            $data['edittime'] = date('Y-m-d H:i:s');
            $data['admin_id'] = $adminId;
            $result = mPinTuan::add($data);
        }

        if (!$result) {
            throw new \Exception('添加失败请重试');
        } else {
            return $id;
        }
    }

    /**
     * 添加拼团sku
     * @param $id
     * @param $sku
     * @param $adminId
     * @return mixed
     * @throws \Exception
     */
    public static function addPinTuanSku($id, $sku, $adminId)
    {
        $sku = array_filter($sku);
        if (!$sku) {
            throw new \Exception('请选择sku');
        }
        $pinTuanSkuList = [];

        $pinTuanInfo = mPinTuan::getInfo(['id' => $id, 'status' => mPinTuan::STATUS_NORMAL]);

        foreach ($sku as $skuId) {
            $pinTuanSkuList[] = [
                'pintuan_id' => $pinTuanInfo['id'],
                'goods_id' => $pinTuanInfo['goods_id'],
                'sku_id' => $skuId,
                'shop_id' => $pinTuanInfo['shop_id'],
                'start_time' => $pinTuanInfo['start_time'],
                'end_time' => $pinTuanInfo['end_time'],
                'addtime' => date('Y-m-d H:i:s'),
                'edittime' => date('Y-m-d H:i:s'),
            ];
        }
        $id = mPinTuanSku::add($pinTuanSkuList);

        if (!$id) {
            throw new \Exception('添加失败请重试');
        } else {
            return $id;
        }

    }

    /**
     * 修改sku
     * @param $activityId
     * @param $pinTuanSkuId
     * @param $param
     * @return mixed
     * @throws \Exception
     */
    public static function saveSku($activityId, $pinTuanSkuId, $param)
    {
        if (count($param) < 4) {
            throw new \Exception('请补全信息');
        }

        if($param['activity_price'] <= 0) {
            throw new \Exception('价格不能小于等于0元');
        }

        $info = mPinTuan::getInfo(['id' => $activityId, 'status' => mPinTuan::STATUS_NORMAL]);
        $skuInfo = sSku::Sku([$info['goods_id']], sSku::FIELDS_GROUP_SKU_INFO, ['activity_type' => ActivityInfo::ACTIVITY_TYPE_PINTUAN]);

        $list = [];
        foreach ($skuInfo as $sku) {
            foreach ($sku as $item) {
                $list[$item['sku_id']] = $item;
            }
        }

        if (!ctype_digit($param['activity_stock']) || $list[$param['sku_id']]['stock'] < $param['activity_stock'] || ($param['activity_stock'] % $info['group_member_num']) != 0 || $param['activity_stock'] <= 0) {
            throw new \Exception('库存设置有误或sku库存不足');
        }

        $goodsInfo[$info['goods_id']] = [$param['sku_id'] => $param['activity_price']];
        $time = ['start_time' => $info['start_time'], 'end_time' => $info['end_time']];
        $result = AllactivityInfoBak::admCheckActivityPrice($goodsInfo, $time, AllactivityInfoBak::ACTIVITY_TYPE_PINTUAN);

        if (end($result)) {
            $invalidPriceData = end($result);
            $skuInfos = sSku::Sku([$info['goods_id']], sSku::FIELDS_GROUP_ALL);
            $skuInfos = array_column($skuInfos[$info['goods_id']]['sku_info'], null, 'sku_id');

            $invalidPriceData = reset($invalidPriceData);
            throw new \Exception('sku ' . $skuInfos[$invalidPriceData['sku_id']]['sku_desc'] . ' 的拼团价格 ' . $param['activity_price'] . ' 高于同时段的商家直降价 ' . $invalidPriceData['activity_price'] . ' ，请修改后再保存');
        }

        // 组装数据
        $pinTuanSkuInfo = [
            'activity_price' => $param['activity_price'],
            'activity_stock' => $param['activity_stock'],
            'actual_stock' => $param['activity_stock'],
            'status' => mPinTuanSku::STATUS_NORMAL,
            'edittime' => date('Y-m-d H:i:s'),
        ];

        $allGoodsInfo = self::formatGoodsInfo($activityId, $info['goods_id'], $info['shop_id'], $info);

        $allSkuInfo = self::formatSkuInfo($activityId, $info, $param, $list);

        $result = mPinTuanSku::action(function () use ($activityId, $pinTuanSkuId, $pinTuanSkuInfo, $allGoodsInfo, $allSkuInfo) {
            $id = mPinTuanSku::save($pinTuanSkuId, $pinTuanSkuInfo);
            if (!$id) {
                return false;
            }

            $result = AllActivityInfo::admUpdateActivityInfo($activityId, AllactivityInfoBak::ACTIVITY_TYPE_PINTUAN, $allGoodsInfo, $allSkuInfo, AllactivityInfoBak::ALL_ACTIVITY_TYPE_PINTUAN, false);

            return $result;
        });

        if (!$result) {
            throw new \Exception('添加失败请重试');
        }

        return $result;
    }

    /**
     * 删除一场拼团
     * @param $id
     * @param $adminId
     * @return mixed
     */
    public static function delPinTuan($id, $adminId)
    {
        $result = mPinTuan::action(function () use ($id, $adminId) {
            $pinTuan = mPinTuan::save($id, ['status' => mPinTuan::STATUS_DELETE, 'admin_id' => $adminId]);
            if (!$pinTuan) {
                return false;
            }

            if (mPinTuanSku::getInfo(['pintuan_id' => $id])) {
                $pinTuanSku = mPinTuanSku::saveByPinTuanId($id, ['status' => mPinTuanSku::STATUS_DELETE]);
                if (!$pinTuanSku) {
                    return false;
                }
            }

            if (mPinTuanSku::getInfo(['pintuan_id' => $id, 'activity_price[>]' => 0])) {
                $result = AllActivityInfo::admDelActivityInfo($id, AllActivityInfo::ACTIVITY_TYPE_PINTUAN, AllActivityInfo::ALL_ACTIVITY_TYPE_PINTUAN);
                return $result;
            }

            return true;
        });

        return $result;
    }

    /**
     * 删除一个sku
     * @param $id
     * @param $pinTuanSkuId
     * @return mixed
     */
    public static function delPinTuanSku($id, $pinTuanSkuId)
    {
        return mPinTuanSku::action(function () use ($id, $pinTuanSkuId) {
            $pinTuanSkuId = mPinTuanSku::save($pinTuanSkuId, ['status' => mPinTuanSku::STATUS_DELETE]);
            if (!$pinTuanSkuId) {
                return false;
            }

            $info = mPinTuanSku::getInfo(['id' => $pinTuanSkuId, 'activity_price[>]' => 0]);
            if($info) {
                $result = AllActivityInfo::admDelActivityBySkuId($id, AllActivityInfo::ACTIVITY_TYPE_PINTUAN, $info['goods_id'], $info['sku_id'], AllActivityInfo::ALL_ACTIVITY_TYPE_PINTUAN);
                return $result;
            }
            return true;
        });
    }

    public static function getPinTuanSkuInfo($pinTuanSkuId)
    {
        $info = mPinTuanSku::getInfo(['id' => $pinTuanSkuId, 'status' => mPinTuanSku::STATUS_NORMAL]);
        $skuInfo = sSku::Sku($info['goods_id'], sSku::FIELDS_GROUP_ALL);
        $info['ori_price'] = 0;
        if(array_column($skuInfo['sku_info'], null, 'sku_id')) {
            $info['ori_price'] = array_column($skuInfo['sku_info'], null, 'sku_id')[$info['sku_id']]['ori_price'];
        }
        return $info;
    }

    /**
     * 生成all_activity_goods表数据
     * @param $id
     * @param $goodsId
     * @param $shopId
     * @param $data
     * @param $status
     * @return array
     */
    public static function formatGoodsInfo($id, $goodsId, $shopId, $data)
    {
        $activityGoods[] = [
            'activity_id' => $id,
            'type' => ActivityInfo::ACTIVITY_TYPE_PINTUAN,
            'channel' => implode(',', self::APP_TYPE),
            'shop_id' => $shopId,
            'goods_id' => $goodsId,
            'start_time' => $data['start_time'],
            'end_time' => $data['end_time'],
            'real_start_time' => $data['start_time'],
            'real_end_time' => $data['end_time'],
            'status' => mPinTuan::STATUS_NORMAL,
        ];

        return $activityGoods;
    }

    public static function formatSkuInfo($activityId, $info, $param, $list)
    {
        $allSkuInfo[] = [
            'activity_id' => $activityId,
            'type' => AllactivityInfoBak::ACTIVITY_TYPE_PINTUAN,
            'channel' => implode(',', self::APP_TYPE),
            'shop_id' => $info['shop_id'],
            'goods_id' => $info['goods_id'],
            'sku_id' => $param['sku_id'],
            'is_activity_stock' => mAllActivitySku::USE_ACTIVITY_STOCK,
            'activity_stock' => $param['activity_stock'],
            'actual_stock' => $param['activity_stock'],
            'activity_price' => $param['activity_price'],
            'limit_buy_count' => 1,
            'activity_allowance' => $info['type'] == mPinTuan::TYPE_NEW ? $list[$param['sku_id']]['ori_price'] - $param['activity_price'] : 0.00,
            'start_time' => $info['start_time'],
            'end_time' => $info['end_time'],
            'real_start_time' => $info['start_time'],
            'real_end_time' => $info['end_time'],
            'status' => mAllActivitySku::STATUS_NORMAL,
        ];
        return $allSkuInfo;
    }

    /**
     * 获取sku列表
     * @param $id
     * @return array
     */
    public static function getSkuList($id)
    {
        $selectSku = [];
        $pinTuanInfo = mPinTuan::get('*', ['id' => $id, 'status' => mPinTuan::STATUS_NORMAL]);
        if ($pinTuanInfo['end_time'] > date('Y-m-d H:i:s')) {
            $list = mPinTuanSku::select('*', ['pintuan_id' => $id, 'status' => mPinTuanSku::STATUS_NORMAL]);
            $selectedSkuId = array_column($list, 'sku_id');

            $skuList = sSku::sku($pinTuanInfo['goods_id'], sSku::FIELDS_GROUP_ALL);
            $skuList = $skuList['sku_info'];
            $skuList = array_column($skuList, null, 'sku_id');
            $skuIds = array_values(array_diff(array_keys($skuList), $selectedSkuId));

            foreach ($skuIds as $skuId) {
                foreach ($skuList as $info) {
                    if ($skuId == $info['sku_id'] && $info['stock'] > 0) {
                        $selectSku[$info['sku_id']] = 'sku_id ' . $info['sku_id'] . ' sku名称 ' . $info['sku_desc'];
                    }
                }
            }
        }

        return ['select_sku' => $selectSku, 'info' => $pinTuanInfo];
    }


    private static function checkExcelItem($params)
    {
        $params['goods_id'] = trim($params['goods_id']);
        $params['sku_id'] = trim($params['sku_id']);
        $params['sort'] = intval($params['sort']);
        $params['price'] = trim($params['price']);
        $params['stock'] = trim($params['stock']);
        $params['start_time'] = trim($params['start_time']);
        $params['end_time'] = trim($params['end_time']);
        $params['type'] = intval($params['type']);
        $params['member'] = intval($params['member']);
        $params['cycle'] = intval($params['cycle']);

        if ($params['start_time'] != date("Y-m-d H:i:s", strtotime($params['start_time']))) {
            throw new \Exception('开始时间格式为:年-月-日 时:分:秒');
        }
        if ($params['end_time'] != date("Y-m-d H:i:s", strtotime($params['end_time']))) {
            throw new \Exception('开始时间格式为:年-月-日 时:分:秒');
        }

        $goodsId = $params['goods_id'];
        $goodsInfo = sGoods::goods([$goodsId], sGoods::FIELDS_GROUP_BASE);
        if (empty($goodsInfo)) {
            throw new \Exception('商品不存在,商品id:' . $goodsId);
        }

        if (!in_array($goodsInfo[$goodsId]['status'], mGoodsBasicInfo::STATUS_APP_NORMAL_MAP)) {
            throw new \Exception('请检查商品状态');
        }
        $params['shop_id'] = isset($goodsInfo[$goodsId]['shop_id']) ? $goodsInfo[$goodsId]['shop_id'] : 0;
        if(!key_exists($params['type'], \Biz\model\activity\PinTuan::TYPE_DESC)) {
            throw new \Exception('拼团类型支持 1:新人团 2:GMV团');
        }

        if ($params['start_time'] > $params['end_time']) {
            throw new \Exception('开始时间大于结束时间');
        }

        if($params['member'] < 2) {
            throw new \Exception('最少2人成团');
        }

        if (!isset($params['icon']) && !key_exists($params['member'], self::ICON_MAP)) {
            throw new \Exception('默认支持2~5人成团');
        }

        $params['icon'] = isset($params['icon']) ? trim($params['icon']) : self::ICON_MAP[$params['member']];

        if (!$params['cycle'] || $params['cycle'] < 0) {
            throw new \Exception('成团周期必须为数字且大于0');
        }

        if ((strtotime($params['start_time']) - time()) < 600) {
            throw new \Exception('开始时间必须在当前时间十分钟之后,活动开始时间:'.$params['start_time']);
        }

        return $params;
    }

    public static function checkExcelData($data)
    {
        if (empty($data)) {
            throw new \Exception('导入的数据为空');
        }
        //检查每项数据的必要性
        $rowData = [];
        $rowCheckDate = [];
        $checkDateArr = [];
        $existsList = [];
        $goodsData = [];
        foreach ($data as $key => $item) {
            $i = $key + 2;
            try {
                foreach (self::EXCEL_TPL_CHECK_MAP as $keyName => $field) {
                    if (!isset($item[$field])) {
                        throw new \Exception("{$keyName} 值未设置");
                    }
                }
                $item['i'] = $i;
                $item = self::checkExcelItem($item);

                //活动时间记录
                $goods_id = $item['goods_id'];
                $sku_id = $item['sku_id'];
                $start_time = $item['start_time'];
                $end_time = $item['end_time'];
                $checkDateArr[$goods_id][$i] = [
                    'start_time' => $start_time,
                    'end_time' => $end_time,
                ];

                //成团周期数值转换
                $item['cycle'] = $item['cycle'] * 3600;

                //查询已经入库的活动
                if (!isset($existsList[$goods_id])) {
                    $where = ['goods_id' => $item['goods_id'], 'status' => mPinTuan::STATUS_NORMAL, 'end_time[>=]' => date('Y-m-d H:i:s')];
                    $existsList[$goods_id] = mPinTuan::getList($where);
                }

                foreach ($existsList[$goods_id] as $info) {
                    $result = tCheckActivityTime::simpleRules(['start_time' => $info['start_time'], 'end_time' => $info['end_time']], ['start_time' => $start_time, 'end_time' => $end_time]);
                    if(! $result) {
                        throw new \Exception("商品已入选了活动{$info['id']}与本活动的时间重叠，不能加入");
                    }
                }

                //将数据分组
                if (!isset($rowData[$goods_id][$start_time . $end_time][$sku_id])) {
                    $goodsData[$goods_id][$start_time . $end_time] = $item;
                    $rowData[$goods_id][$start_time . $end_time][$sku_id] = $item;
                    $rowCheckDate[$item['goods_id']][$start_time . $end_time] = $item;
                } else {
                    throw new \Exception("配置重复");
                }
            } catch (\Exception $e) {
                throw new \Exception("第{$i}行,错误信息:".$e->getMessage());
            }
        }

        //检查excel数据之间活动时间冲突情况
        foreach ($rowCheckDate as $goods_id => $arr) {
            foreach ($arr as $item) {
                $tempCheck = isset($checkDateArr[$goods_id]) ? $checkDateArr[$goods_id] : [];
                foreach ($tempCheck as $i => $v) {
                    if (($v['start_time'] == $item['start_time']) && ($v['end_time'] == $item['end_time'])) {
                        //时间一致表示
                    } else {
                        $result = tCheckActivityTime::simpleRules($v, $item);
                        if(! $result) {
                            throw new \Exception("第{$i}行,时间配置有冲突");
                        }
                    }
                }
            }
        }

        return compact('goodsData', 'rowData');
    }

    const ICON_MAP = [
        2 => 'eedcc302o0100iu00ppuhes',//2人：http://img.hhz1.cn/Op-imageShow/eedcc302o0100iu00ppuhes?iv=1
        3 => '16fc2302o0100xY00ppuhes',//3人：http://img.hhz1.cn/Op-imageShow/16fc2302o0100xY00ppuhes?iv=1
        4 => 'ebc8a302o0100f900ppuhes',//4人：http://img.hhz1.cn/Op-imageShow/ebc8a302o0100f900ppuhes?iv=1
        5 => 'fcffd302o0100a800ppuhes',//5人：http://img.hhz1.cn/Op-imageShow/fcffd302o0100a800ppuhes?iv=1
    ];

    const EXCEL_TPL_CHECK_MAP = [
        '商品ID'    => 'goods_id',
        'skuId'     => 'sku_id',
        '拼团类型'  => 'type',
        '成团人数'  => 'member',
        '成团周期'  => 'cycle',
        '开始时间'  => 'start_time',
        '结束时间'  => 'end_time',
        '排序'      => 'sort',
        '活动价'    => 'price',
        '活动库存'  => 'stock',
    ];

    const EXCEL_TPL_HEADER_MAP = [
        '商品ID'    => 'goods_id',
        'skuId'     => 'sku_id',
        '拼团类型'  => 'type',
        '成团人数'  => 'member',
        '成团周期'  => 'cycle',
        '开始时间'  => 'start_time',
        '结束时间'  => 'end_time',
        '排序'      => 'sort',
        '拼团icon'  => 'icon',
        '活动价'    => 'price',
        '活动库存'  => 'stock',
    ];
    public static function getExcelData($file)
    {
        $data = (new ReadExcel($file))->getExcelData(0, self::EXCEL_TPL_HEADER_MAP, false);
        return $data;

    }

    /**
     * 下载拼团活动模板
     * @throws \Exception
     */
    public static function getExcelTpl()
    {
        $header = array_keys(self::EXCEL_TPL_HEADER_MAP);

        $downList[] = [
            616880,
            2228709,
            '1:新人团 2:GMV团',
            '最少2人成团',
            '单位:小时',
            '开始时间的格式为 : 2019-01-30 00:00:00',
            '结束时间的格式为 : 2019-12-01 00:00:00',
            '1',
            '3540c30by0by0R800ppshry',
            '25.00',
            '10',
        ];
        $letter = range('A', 'K');

        $excelObj = new DownExcel();
        $excelObj->setFileName('拼团活动模板');
        $excelObj->setSheetTitle('拼团活动模板');
        //设置列宽

        //默认单元格居中
        $excelObj->setDefaultStyleAlignment();
        $default = 20;
        $widths = ['I' => 50];
        foreach ($letter as $value) {
            $widths[$value] = isset($widths[$value]) ? $widths[$value] : $default;
        }

        $excelObj->setWidth($widths);

        $excelObj->create($downList, $header);
    }
}